#!/bin/bash

if [[ -z ${AWS_SHARED_CREDENTIALS_FILE} ]]; then
  SIMULATOR_AWS_CREDS_PATH="${HOME}/.aws/"
else
  SIMULATOR_AWS_CREDS_PATH="$(dirname "${AWS_SHARED_CREDENTIALS_FILE}")"
fi

CONTAINER_NAME="controlplane/simulator:latest"
KUBE_SIM_TMP="${HOME}/.kubesim/"
SIMULATOR_CONFIG_FILE="${KUBE_SIM_TMP}simulator.yaml"
BASTION_TF_VARS="${KUBE_SIM_TMP}settings/bastion.tfvars"

[[ ! -d ${KUBE_SIM_TMP} ]] && mkdir -pv "${KUBE_SIM_TMP}/settings"

touch "${SIMULATOR_CONFIG_FILE}"
touch "${BASTION_TF_VARS}"

docker pull "${CONTAINER_NAME}"

docker run \
  -h launch \
  -v "${SIMULATOR_AWS_CREDS_PATH}":/home/launch/.aws \
  -v "${KUBE_SIM_TMP}":/home/launch/.kubesim \
  -v "${KUBE_SIM_TMP}":/home/launch/.ssh \
  -e "AWS_ACCESS_KEY_ID" \
  -e "AWS_SECRET_ACCESS_KEY" \
  -e "AWS_REGION" \
  -e "AWS_DEFAULT_REGION" \
  -e "AWS_PROFILE" \
  -e "AWS_DEFAULT_PROFILE" \
  --rm --init -it ${CONTAINER_NAME}
